package top.oylan.manager.tools;

import top.oylan.manager.entity.api.BaseApi;

import java.io.Serializable;

public class ApiUtils implements Serializable {

    //接口报错后返回信息
    public static final String SERVER_ERROR = "服务器异常，请稍后再试";

    //接口查询数据成功
    public static final String SUCCESS = "获取成功";
    //未登录
    public static final String NOTLOGIN = "未登录";
    //未购买
    public static final String NOTBUG= "未购买";
    /**
     * @param baseApi
     * @param msg     错误信息
     * @return
     * @discription 添加失败信息
     * @author zhl
     * @date 2018/5/14 15:16
     */
    public static BaseApi addFialMessage(BaseApi baseApi, String msg) {
        if (baseApi != null) {
            baseApi.setCode(400);
            baseApi.setMsg(msg);
        }
        return baseApi;
    }

    /**
     * @param baseApi 目标jsonobject
     * @param msg     成功信息
     * @param data    成功数据
     * @return 参数jsonObject
     * @discription 接口请求成功返回数据
     * @author pwj
     * @date 2016/7/25 11:37
     */
    public static BaseApi addRightData(BaseApi baseApi, String msg, Object data) {
        if (baseApi != null) {
            baseApi.setCode(200);
            if (isNotBlank(msg)) {
                baseApi.setMsg(msg);
            } else {
                baseApi.setMsg(SUCCESS);
            }
            if (data == null){
                baseApi.setData("");
            }else {
                baseApi.setData(data);
            }
        }
        return baseApi;
    }

    public static BaseApi notLogin(BaseApi baseApi, String msg, Object data) {
        if (baseApi != null) {
            baseApi.setCode(205);
            if (isNotBlank(msg)) {
                baseApi.setMsg(msg);
            } else {
                baseApi.setMsg(NOTLOGIN);
            }
            if (data == null){
                baseApi.setData("");
            }else {
                baseApi.setData(data);
            }
        }
        return baseApi;
    }
    public static BaseApi notBuy(BaseApi baseApi, String msg, Object data) {
        if (baseApi != null) {
            baseApi.setCode(206);
            if (isNotBlank(msg)) {
                baseApi.setMsg(msg);
            } else {
                baseApi.setMsg(NOTBUG);
            }
            if (data == null){
                baseApi.setData("");
            }else {
                baseApi.setData(data);
            }
        }
        return baseApi;
    }
    /**
     * @discription  接口请求成功返回数据（重载）
     * @author pwj
     * @date 2016/9/6 10:23
     * @param baseApi
     * @param msg
     * @return
     */
    public static BaseApi addRightData( BaseApi baseApi , String msg ){
        if(baseApi != null){
            baseApi.setCode(200);
            if(isNotBlank(msg)){
                baseApi.setMsg(msg);
            }else{
                baseApi.setMsg(SUCCESS);
            }
        }
        return baseApi;
    }

    private static boolean isNotBlank(String string){
        if(null != string && !string.equals("")){
            return true;
        }
        return false;
    }
}
